package main.动态规划;

/**
 * @author tmh
 * @date 2024/9/4 21:43
 * @description
 */
public class T1143最长公共子序列 {
    /**
     * 有点背诵吟唱的感觉
     * @param text1
     * @param text2
     * @return
     */
    public int longestCommonSubsequence(String text1, String text2) {
        int m = text1.length();
        int n = text2.length();
        // dp[i+1][j+1]表示text1[0,i]范围和text2[0,j]范围构成的最长公共子序列，初始都为0
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                dp[i+1][j+1] = text1.charAt(i) == text2.charAt(j) ? 1 + dp[i][j] : Math.max(dp[i ][j+1], dp[i+1][j]);
            }
        }
        return dp[m][n];
    }
}
